package br.unifor.g2cl.hl;

import java.net.SocketAddress;

/**
 * Class used to correlate a request to a member response when sending synchronous messages to the group. 
 * 
 * @author Leandro Sales (leandro.shp@gmail.com)
 *
 */
public class Rsp {

	boolean received = false;

	boolean suspected = false;

	SocketAddress sender = null;

	Object retval = null;

	public Rsp(SocketAddress sender) {
		this.sender = sender;
	}

	public Rsp(SocketAddress sender, boolean suspected) {
		this.sender = sender;
		this.suspected = suspected;
	}

	public Rsp(SocketAddress sender, Object retval) {
		this.sender = sender;
		this.retval = retval;
		received = true;
	}

	public boolean equals(Object obj) {
		if (!(obj instanceof Rsp))
			return false;
		Rsp other = (Rsp) obj;
		if (sender != null)
			return sender.equals(other.sender);
		return other.sender == null;
	}

	/**
	 * Return the response, related to the request, sent by the member represented by the Sender attr.
	 */
	public Object getValue() {
		return retval;
	}

	public void setValue(Object val) {
		this.retval = val;
	}

	/**
	 * 
	 * Represents the sender of this response
	 */
	public SocketAddress getSender() {
		return sender;
	}

	/** Represents whether the response was received */
	public boolean wasReceived() {
		return received;
	}

	public void setReceived(boolean received) {
		this.received = received;
		if (received) {
			suspected = false;
		}
	}

	/** Represents whether the response was suspected */
	public boolean wasSuspected() {
		return suspected;
	}

	public void setSuspected(boolean suspected) {
		this.suspected = suspected;
		if (suspected) {
			received = false;
		}
	}

	public String toString() {
		return new StringBuilder("sender=").append(sender).append(", retval=")
				.append(retval).append(", received=").append(received).append(
						", suspected=").append(suspected).toString();
	}

}
